﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// ResourceCategories 类。
	/// </summary>
	public static partial class ResourceCategories
	{
		#region GetIdentifier
		/// <summary>
		/// 根据指定的信息获取类别标识。
		/// </summary>
		/// <param name="o">类别的集合。</param>
		/// <param name="categoryCode">类别代码。</param>
		/// <returns>指定的类别的标识。</returns>
		public static Guid? GetIdentifier(this System.Data.Linq.Table<ResourceCategory> o, string categoryCode)
		{
			var q = from r in o
					where r.CategoryCode == categoryCode
					select (Guid?)r.CategoryId;
			return q.SingleOrDefault();
		}
		#endregion




		#region Exists
		/// <summary>
		/// 验证是否存在指定的类别标识。
		/// </summary>
		/// <param name="id">进行难的标识。</param>
		/// <returns>返回一个布尔值表示指定的类别标识是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<ResourceCategory> o, Guid id)
		{
			return o.Any(p => p.CategoryId == id);
		}

		/// <summary>
		/// 验证是否存在指定的类别代码。
		/// </summary>
		/// <param name="categoryCode">进行验证的类别代码。</param>
		/// <returns>返回一个布尔值表示指定的类别代码是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<ResourceCategory> o, string categoryCode)
		{
			return o.Any(p => p.CategoryCode == categoryCode);
		}
		#endregion



		#region GetItems
		public static List<ResourceCategory> GetItems()
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ResourceCategories.GetItems();
		}

		public static List<ResourceCategory> GetItems(this System.Data.Linq.Table<ResourceCategory> o)
		{
			return o.ToList();
		}
		#endregion



		#region GetItem
		/// <summary>
		/// 根据类别标识获取指定的类别。
		/// </summary>
		/// <param name="o">资源类别集合。</param>
		/// <param name="id">资源标识。</param>
		/// <returns>指定的类别。</returns>
		public static ResourceCategory GetItem(this System.Data.Linq.Table<ResourceCategory> o, Guid id)
		{
			return o.SingleOrDefault(p => p.CategoryId == id);
		}

		/// <summary>
		/// 根据类别代码获取指定的类别。
		/// </summary>
		/// <param name="o">资源类别集合。</param>
		/// <param name="categoryCode">类别代码。</param>
		/// <returns>指定的类别。</returns>
		public static ResourceCategory GetItem(this System.Data.Linq.Table<ResourceCategory> o, string categoryCode)
		{
			return o.SingleOrDefault(p => p.CategoryCode == categoryCode);
		}
		#endregion
	}



	/// <summary>
	/// ResourceCategories 类操作部分。
	/// </summary>
	public static partial class ResourceCategories
	{
		#region Register
		/// <summary>
		/// 系统管理资源类别标识。
		/// </summary>
		public static readonly Guid SystemManageResourceCategoryId = new Guid("63FECE7C-74B8-4BE9-8622-101EABC2C2E7");

		/// <summary>
		/// 系统管理资源类别代码。
		/// </summary>
		public const string SystemManageResourceCategoryCode = "SystemManage";

		/// <summary>
		/// 系统管理资源类别名称。
		/// </summary>
		public const string SystemManageResourceCategoryName = "系统管理";

		/// <summary>
		/// 系统管理资源类别描述。
		/// </summary>
		public const string SystemManageResourceCategoryDescription = "";





		/// <summary>
		/// 注册资源类别。
		/// </summary>
		/// <param name="registerCategory">注册的资源类别。</param>
		/// <returns>注册的资源类别。</returns>
		internal static ResourceCategory Register(ResourceCategory registerCategory)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ResourceCategory category = dc.ResourceCategories.GetItem(registerCategory.CategoryCode);

			if (category == null)
			{
				category = registerCategory.ToNewEntity<ResourceCategory>();
				dc.ResourceCategories.InsertOnSubmit(category);
				dc.SubmitChanges();
			}
			else
			{
				if (category.CategoryName != registerCategory.CategoryName || category.Description != registerCategory.Description)
				{
					// 修正数据，以确保数据的正确性
					category.CategoryName = registerCategory.CategoryName;
					category.Description = registerCategory.Description;

					try
					{
						dc.SubmitChanges(ConflictMode.ContinueOnConflict);
					}
					catch (ChangeConflictException e)
					{
						foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
							occ.Resolve(RefreshMode.KeepCurrentValues);
					}
				}
			}

			// 返回只读数据。
			return registerCategory.FillByEntity(category);
		}

		/// <summary>
		/// 注册系统管理角色。
		/// </summary>
		/// <returns></returns>
		internal static ResourceCategory RegisterSystemManage()
		{
			ResourceCategory category = new ResourceCategory();
			category.CategoryId = SystemManageResourceCategoryId;
			category.CategoryName = SystemManageResourceCategoryName;
			category.CategoryCode = SystemManageResourceCategoryCode;
			category.Description = SystemManageResourceCategoryDescription;

			return Register(category);
		}
		#endregion



		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, ResourceCategory resourceCategory)
		{
			Resources.DeleteByParent(dc, resourceCategory);	// 删除关联数据
			dc.ResourceCategories.DeleteOnSubmit(resourceCategory);
		}
		#endregion
		#endregion
	}
}